<?php
	include_once('dataBaseConnection.php');
	include_once('getDateInformation.php');
	$date = date("d-m-Y");
	$cnt = 0;
	$cnt2 = 0;
	$lateUsers = array();
	$updatedUsers = array();
	$query = "SELECT user, name FROM users"; 
    $result = mysqli_query($connection, $query); 
	while ($row = mysqli_fetch_array($result)) {
      if (strtotime(getLastMonthLastChargeDay($row['user'], $date)) < strtotime(getLastMonthEndingDay($date)) && strcmp(getLastChargeDate($row['user']), "no hay cargas registradas") != 0) {
	   	$lateUsers[$cnt] =  $row['user'];
		$cnt++;
		
	  }else{
	  	$updatedUsers[$cnt2] =  $row['user'];
		$cnt2++;
	  }
	  
    }
	$query = "SELECT user, name FROM users"; 
    $result = mysqli_query($connection, $query); 
	while ($row = mysqli_fetch_array($result)) {
      for ($i = 0; $i < count($lateUsers); $i++) {
		if (strcmp($row['user'], $lateUsers[$i]) == 0) {
			$lateUser = $row['user'];
			$update = mysqli_query($connection, "UPDATE users SET updated = '0' WHERE user='$lateUser'"); 		
		}
	  }
    }
	
	$query = "SELECT user, name FROM users"; 
    $result = mysqli_query($connection, $query); 
	while ($row = mysqli_fetch_array($result)) {
      for ($i = 0; $i < count($updatedUsers); $i++) {
		if (strcmp($row['user'], $updatedUsers[$i]) == 0) {
			$updatedUser = $row['user'];
			$update = mysqli_query($connection, "UPDATE users SET updated = '1' WHERE user='$updatedUser'"); 		
		}
	  }
    }
	
?>